package org.example.game.history;

import java.util.Arrays;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/08 22:43
 */
public class Solution100324 {

    public static void main(String[] args) {
        Solution100324 solution100324 = new Solution100324();
        System.out.println(solution100324.clearDigits("a0id5"));
    }

    public String clearDigits(String s) {

        boolean[] idDel = new boolean[s.length()];
        Arrays.fill(idDel, Boolean.FALSE);
        int numCount = 0;
        int lastNumIndex = 0;
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(Character.isDigit(chars[i])){
                idDel[i] = Boolean.TRUE;
                numCount++;
                lastNumIndex = i;
            }
        }

        int count = 0;
        for(int j=lastNumIndex;j>=0;j--){
            if(!idDel[j] && count>0){
                idDel[j] = Boolean.TRUE;
                numCount--;
                count--;
            }else if(idDel[j]){
                count++;
            }

            if(numCount == 0){
                break;
            }
        }

        StringBuilder builder =  new StringBuilder();

        for (int i = 0; i < chars.length; i++) {
            if(!idDel[i]){
                builder.append(chars[i]);
            }
        }

        return builder.toString();

    }

}
